package org.zoomdev.zoom.common.filter.impl;

import org.zoomdev.zoom.common.filter.Filter;

import java.util.Collection;


/**
 * 所有过滤器中，必须所有的过滤器都通过，才算通过
 *
 * @param <T>
 * @author jzoom
 */
public final class AndFilter<T> implements Filter<T> {

    private final Filter<T>[] filters;

    public AndFilter(Filter<T>... filters) {
        this.filters = filters;
    }

    public AndFilter(Collection<Filter<T>> filters) {
        this.filters = new Filter[filters.size()];
        int index = 0;
        for (Filter<T> filter : filters) {
            this.filters[index++] = filter;
        }
    }

    @Override
    public boolean accept(T value) {
        for (Filter<T> filter : filters) {
            if (!filter.accept(value)) {
                return false;
            }
        }
        return true;
    }

}
